package com.csii.core.system.vo;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 在线用户信息
 * </p>
 *
 * @Author ian
 * @since 2018-12-20
 */
@Data
@EqualsAndHashCode(callSuper = false)
//@Accessors(chain = true)
public class LoginUser implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 登录人id
	 */
	private String id;

	/**
	 * 登录人账号
	 */
	private String username;

	/**
	 * 登录人名字
	 */
	private String realname;

	/**
	 * 登录人密码
	 */
	private String password;

	/**
	 * 当前登录部门code
	 */
	private String orgCode;
	/**
	 * 头像
	 */
	private String avatar;

	/**
	 * 生日
	 */
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date birthday;

	/**
	 * 性别（1：男 2：女）
	 */
	private Integer sex;

	/**
	 * 电子邮件
	 */
	private String email;

	/**
	 * 电话
	 */
	private String phone;

	/**
	 * 状态(1：正常 2：冻结 ）
	 */
	private Integer status;

	private Integer delFlag;
	/**
	 * 同步工作流引擎1同步0不同步
	 */
	private Integer activitiSync;

	/**
	 * 创建时间
	 */
	private Date createTime;

	/**
	 * 身份（1 普通员工 2 上级）
	 */
	private Integer userIdentity;

	/**
	 * 管理部门ids
	 */
	private String departIds;

	/**
	 * 职务，关联职务表
	 */
	private String post;

	/**
	 * 座机号
	 */
	private String telephone;

	/**
	 * 多租户id配置，编辑用户的时候设置
	 */
	private String relTenantIds;

	private String openId;

	private String openType;

	private String accessToken;

	private String refreshToken;

	// 1是超级管理 0不是
	private String isSuperAdmin;
	//职位
	private String postName;
	//工号
	private String workNum;
	//部门地址
	private String address;
	//部门名称
	private String orgName;

	public static LoginUser build(JSONObject principal) {
		LoginUser loginUser = new LoginUser();
		loginUser.setId(principal.getString("userId"));
		loginUser.setEmail(principal.getString("email"));
		loginUser.setAvatar(principal.getString("avatar"));
		loginUser.setUsername(principal.getString("userName"));
		loginUser.setPhone(principal.getString("phone"));
		loginUser.setRealname(principal.getString("nickName"));
		loginUser.setOpenId(principal.getString("openId"));
		loginUser.setOpenType(principal.getString("openType"));
		loginUser.setRelTenantIds(principal.getString("tenantId"));
		loginUser.setUserIdentity(principal.getInteger("userIdentity"));
		loginUser.setDepartIds(principal.getString("departIds"));
		loginUser.setIsSuperAdmin(principal.getString("isSuperAdmin"));
		loginUser.setOrgCode(principal.getString("orgCode"));
		loginUser.setCreateTime(new Date(Long.parseLong(principal.getString("createTime"))));
		loginUser.setPostName(principal.getString("postName"));
		loginUser.setWorkNum(principal.getString("workName"));
		return loginUser;
	}

	public static LoginUser build(Map<String, Object> principal) {
		LoginUser loginUser = new LoginUser();
		loginUser.setId(getString(principal.get("userId")));
		loginUser.setEmail(getString(principal.get("email")));
		loginUser.setAvatar(getString(principal.get("avatar")));
		loginUser.setUsername(getString(principal.get("userName")));
		loginUser.setPhone(getString(principal.get("phone")));
		loginUser.setRealname(getString(principal.get("nickName")));
		loginUser.setOpenId(getString(principal.get("openId")));
		loginUser.setOpenType(getString(principal.get("openType")));
		loginUser.setRelTenantIds(getString(principal.get("tenantId")));
		String userIdentity = getString(principal.get("userIdentity"));
		loginUser.setUserIdentity(StrUtil.isNotEmpty(userIdentity) ? Integer.valueOf(userIdentity) : null);
		loginUser.setDepartIds(getString(principal.get("departIds")));
		loginUser.setIsSuperAdmin(getString(principal.get("isSuperAdmin")));
		loginUser.setOrgCode(getString(principal.get("orgCode")));
		loginUser.setCreateTime(new Date(Long.parseLong(getString(principal.get("createTime")))));
		loginUser.setPostName(getString(principal.get("postName")));
		loginUser.setWorkNum(getString(principal.get("workName")));
		return loginUser;
	}

	private static String getString(Object obj){
		return obj != null ? obj.toString():null;
	}
}
